package com.ewili.x.special.task.thread.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.ExceptionUtils;
import com.ewili.framework.utils.StringUtils;
import com.ewili.x.special.dao.inter.ValueDao;
import com.ewili.x.special.entity.Value;
import com.ewili.x.special.task.thread.inter.ValueThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Slf4j
@Component
public class ValueThreadTaskImpl implements ValueThreadTask {

    @Autowired
    private ValueDao valueDao;

    @Override
    @Async("myTaskAsyncPool")
    public void spideValue(String url, String code) {
        try {
            this.spide(url, code);
        } catch (Exception e) {
            log.error("url:{}", url, e);
        }

    }

    private void spide(String url, String code) {
        Connection connection = Jsoup.connect(url).timeout(100000);
        connection.maxBodySize(0);

        String content = null;
        try {
            content = connection.get().body().text();
        } catch (IOException e) {
            ExceptionUtils.propagate(e);
        }

        if (content.contains("pages:0")) {
            return;
        }
        String subContent = StringUtils.substring(content, "data:[{", ",font:").replace("data:", "");
        JSONArray jsonArray = JSON.parseArray(subContent);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            this.saveData(object, code);
        }
    }

    /**
     * 保存数据
     *
     * @param object
     */
    private void saveData(JSONObject object, String code) {
        Date date = object.getSqlDate("TRADEDATE");
        Double close = "-".equalsIgnoreCase(object.getString("NEW")) ? 0.0d : object.getDouble("NEW");
        if (close == 0.0d) {
            return;
        }
        Double changeRate = "-".equalsIgnoreCase(object.getString("CHG")) ? 0.0d : object.getDouble("CHG");
        Double totalValue = "-".equalsIgnoreCase(object.getString("ZSZ")) ? 0.0d : object.getDouble("ZSZ");
        if (totalValue == 0.0d) {
            return;
        }
        Double circulationValue = object.getDouble("AGSZBHXS");
        Double peTtm = "-".equalsIgnoreCase(object.getString("PE9")) ? 0.0d : object.getDouble("PE9");
        Double pe = "-".equalsIgnoreCase(object.getString("PE7")) ? 0.0d : object.getDouble("PE7");
        Double pb = "-".equalsIgnoreCase(object.getString("PB8")) ? 0.0d : object.getDouble("PB8");
        Double peg = "-".equalsIgnoreCase(object.getString("PEG1")) ? 0.0d : object.getDouble("PEG1");
        Double pcf = "-".equalsIgnoreCase(object.getString("PCFJYXJL9")) ? 0.0d : object.getDouble("PCFJYXJL9");
        Double ps = "-".equalsIgnoreCase(object.getString("PS9")) ? 0.0d : object.getDouble("PS9");
        String hyCode = object.getString("HYCode");

        Boolean isExists = valueDao.existsByCodeAndDate(code, date);
        if (isExists) {
            return;
        }

        Value value = new Value();
        value.setCode(code);
        value.setDate(date);
        value.setClose(close);
        value.setChangeRate(changeRate);
        value.setTotalValue(totalValue);
        value.setCirculationValue(circulationValue);
        value.setPeTtm(peTtm);
        value.setPe(pe);
        value.setPb(pb);
        value.setPeg(peg);
        value.setPcf(pcf);
        value.setPs(ps);
        value.setHyCode(hyCode);

        valueDao.save(value);
    }
}
